package com.shenghui.enterprise.base.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenghui.enterprise.base.entity.BaseUser;
import com.shenghui.enterprise.base.entity.Page;
import com.shenghui.enterprise.utils.GeneralUtil;
import com.shenghui.enterprise.utils.UserUtil;
import com.shenghui.enterprise.vo.Result;
import com.shenghui.enterprise.base.entity.BaseDataDictionaryDetail;
import com.shenghui.enterprise.base.entity.PageModel;
import com.shenghui.enterprise.base.mapper.BaseDataDictionaryDetailMapper;
import com.shenghui.enterprise.base.service.IBaseDataDictionaryDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @Description: 字典子表业务逻辑实现类
 * @Author houjh
 * @Email: happyxiaohou@gmail.com
 * @Date: 2020-06-01
 * @Copyright (c) 2020-2022 云南昇晖科技有限公司
 */
@Service
@Transactional
public class BaseDataDictionaryDetailServiceImpl extends ServiceImpl<BaseDataDictionaryDetailMapper, BaseDataDictionaryDetail> implements IBaseDataDictionaryDetailService {

    @Autowired
    private BaseDataDictionaryDetailMapper dataDictionaryDetailMapper;
    @Autowired
    private UserUtil userUtil;

    @Override
    public PageModel getDataDictionaryDetailList(Map map) {
        map.put("order", "order_value,create_date");
        Page<Map> mapPage = new Page<>(map);
        if(GeneralUtil.isNull(map.get("dicId"))){
            map.put("dicId","0");
        }
        List<Map> list = dataDictionaryDetailMapper.getDataDictionaryDetailList(mapPage, map);
        return new PageModel(mapPage.getTotal(),list);
    }

    /**
     * 根据父级字典code查询子字典列表
     * @param map
     * @return
     */
    @Override
    public PageModel getDataDictionaryDetailListByParentCode(Map map) {
        try {
            map.put("order", "d.order_value,d.create_date");
            Page<Map> mapPage = new Page<Map>(map);
            List<Map> list = dataDictionaryDetailMapper.getDataDictionaryDetailListByParentCode(mapPage, map);
            return new PageModel(mapPage.getTotal(), list);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageModel(0, null);
        }
    }

    @Override
    public Result addDataDictionaryDetail(BaseDataDictionaryDetail dataDictionaryDetail) {
        int count = 0;
        BaseUser logBaseUser = userUtil.getLoginUser();
        if (GeneralUtil.isNotNull(dataDictionaryDetail.getDataDictionaryDetailId())) {    //修改
            //验证code是否重复
            count = dataDictionaryDetailMapper.selectCount(new QueryWrapper<BaseDataDictionaryDetail>().lambda()
                    .eq(BaseDataDictionaryDetail::getCode, dataDictionaryDetail.getCode())
                    .eq(BaseDataDictionaryDetail::getDataDictionaryId, dataDictionaryDetail.getDataDictionaryId())
                    .ne(BaseDataDictionaryDetail::getDataDictionaryDetailId, dataDictionaryDetail.getDataDictionaryDetailId())
            );
            if(count > 0){
                return Result.error("同一字典下，字典详情Code码已存在!");
            }
            dataDictionaryDetailMapper.updateById(dataDictionaryDetail);
        } else {    //新增
            //验证code是否重复
            count = dataDictionaryDetailMapper.selectCount(new QueryWrapper<BaseDataDictionaryDetail>().lambda()
                    .eq(BaseDataDictionaryDetail::getCode, dataDictionaryDetail.getCode())
                    .eq(BaseDataDictionaryDetail::getDataDictionaryId, dataDictionaryDetail.getDataDictionaryId())
            );
            if(count > 0){
                return Result.error("同一字典下，字典详情Code码已存在!");
            }
            if(GeneralUtil.isNotNull(logBaseUser)){
                dataDictionaryDetail.setCreateUserId(logBaseUser.getUserId());
            }
            if (GeneralUtil.isNull(dataDictionaryDetail.getOrderValue())) {
                dataDictionaryDetail.setOrderValue(0);
            }
            dataDictionaryDetail.setCreateDate(LocalDateTime.now());
            dataDictionaryDetail.setState(1);
            dataDictionaryDetailMapper.insert(dataDictionaryDetail);
        }
        return Result.ok("字典详情添加成功");
    }

    @Override
    public Result getDataDictionaryDetailById(String id) {
        if(GeneralUtil.isNull(id)){
            return Result.error("必需参数【id】不能为空!");
        }
        Map<String,Object> result = dataDictionaryDetailMapper.getDictionaryDetailById(id);
        if(result != null && result.size() > 0){
            return Result.ok( "查询成功", result);
        }
        return Result.error("未查询到对应的记录信息!");
    }

    @Override
    public Result deleteDataDictionaryDetail(String id) {
        if(GeneralUtil.isNull(id)){
            return Result.error("必需参数【id】不能为空!");
        }
        BaseDataDictionaryDetail baseDataDictionaryDetail = dataDictionaryDetailMapper.selectById(id);
        baseDataDictionaryDetail.setState(0);
        dataDictionaryDetailMapper.updateById(baseDataDictionaryDetail);
        return Result.ok("删除成功!");
    }

    @Override
    public Result getDicDetailByCode(String code, String codeTwo) {
        BaseDataDictionaryDetail dicDetailByCode = dataDictionaryDetailMapper.getDicDetailByCode(code, codeTwo);
        return Result.ok("查询成功", dicDetailByCode);
    }

    @Override
    public BaseDataDictionaryDetail getBaseDataDictionaryDetailByCode(String code) {
        Assert.hasText(code, "code不能为空!");
        QueryWrapper<BaseDataDictionaryDetail> wrapper = new QueryWrapper<BaseDataDictionaryDetail>();
        wrapper.lambda().eq(BaseDataDictionaryDetail::getCode, code);
        return dataDictionaryDetailMapper.selectOne(wrapper);
    }
}
